//
//  CD_TabbarController.m
//  chineseDictionary
//
//  Created by 如沐春风 on 2022/2/22.
//

#import "CD_TabbarController.h"
#import "BaseNavViewController.h"
#import "AppDelegate.h"

@interface CD_TabbarController ()<UITabBarControllerDelegate>

@end

@implementation CD_TabbarController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    [[UITabBar appearance] setBackgroundColor:[UIColor whiteColor]];
    [UITabBar appearance].translucent = NO; //这句表示取消tabBar的透明效果。
    
    [self setSubController];
    
//    方法1
    self.tabBar.tintColor = [UIColor grayColor];

//    方法2
    if (@available(iOS 10.0, *)) {
        self.tabBar.unselectedItemTintColor = [UIColor grayColor];
    } else {
    // Fallback on earlier versions
    }
}

- (void)setSubController{
    
    NSArray *VCNameArr = @[@"MainViewController",@"GoodsCategoryViewController",@"ShoppingCartViewController",@"MineViewController"];
    NSMutableArray *VCNavArr = [NSMutableArray array];
    BaseNavViewController *VCNav = nil;
    for (NSString *VCName in VCNameArr) {
        UIViewController *VC = [[NSClassFromString(VCName) alloc] init];
        VC.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@""
                                                                               style:UIBarButtonItemStylePlain
                                                                              target:self
                                                                              action:nil];
        VCNav = [[BaseNavViewController alloc] initWithRootViewController:VC];
        [VCNavArr addObject:VCNav];
        
        
    }
    
    [self setViewControllers:VCNavArr];
    
    
    NSArray *tabBarTitleArr  = @[@"首页",@"商品分类",@"购物车",@"个人中心"];
    NSArray *tabBarImgArr = @[@"home",@"category",@"carlist",@"mine"];
    NSArray *tabBarItems = self.tabBar.items;
    
    for (int i = 0; i < tabBarItems.count; i++) {
        UITabBarItem *item = tabBarItems[i];
        item.title = tabBarTitleArr[i];
        item.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%@_sel",tabBarImgArr[i]]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        item.image = [[UIImage imageNamed:[NSString stringWithFormat:@"%@_unsel",tabBarImgArr[i]]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        [item setTitleTextAttributes:@{NSForegroundColorAttributeName:kThemeColor,NSFontAttributeName:[UIFont systemFontOfSize:13]} forState:UIControlStateSelected];
        [item setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor grayColor],NSFontAttributeName:[UIFont systemFontOfSize:13]} forState:UIControlStateNormal];
        
    }
}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    
//    NSLog(@"%@" , item);

}
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    
    return YES;
}


@end
